package com.michaellou.encript;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author conmind
 */
public class Test {
	public static void main(String[] args) {
		try {
			//需要加密的数据
			String data = "456";
			//密钥
			String key = "123";
			our(data,key);
			standard(data, key);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static void our(String data, String key){
		byte[] b = HMACSHA1.getHmacSHA1(data, key);
		String s = new BASE64Encoder().encode(b);
		System.out.println("hmacsha1 = " + s);
	}
	private static void standard(String data, String key) {
        byte[] byteHMAC = null;
        try {
            Mac mac = Mac.getInstance("HmacSHA1");
            SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
            mac.init(spec);
            byteHMAC = mac.doFinal(data.getBytes());
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException ignore) {
        }
        String oauth = new BASE64Encoder().encode(byteHMAC);
        System.out.println("standard = "+oauth);
    }
}


